home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 11 - 1995 / 11.02 Feb 95 / 11.02 Challenge / SolveBottomCorners.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-10  |  5.1 KB  |  176 lines  |  [TEXT/KAHL]

  1. /*
  2.  
  3.     SolveRubiksCube
  4.     Copyright (c) 1994  J Robert Boonstra
  5.         
  6. */
  7.  
  8. #pragma options(honor_register,assign_registers)
  9.  
  10. #include "rubik.h"
  11. #include "transform.h"
  12.  
  13. Boolean  SolveBottomCorners(register RubiksCube *rub)
  14. {
  15. short loopCount;
  16.   
  17. /**********************************************************
  18.  * 
  19.  * STEP 4:  Move corner cubes in bottom layer into position.
  20.  *          (but not necessarily the correct orientation)
  21.  *
  22.  *********************************************************/
  23.   if ( CornerEquals(D,L,B,D,R,B) ) {         D3;
  24.   } else if ( CornerEquals(D,L,F,D,R,B) ) {  D2;
  25.   } else if ( CornerEquals(D,R,F,D,R,B) ) {  D1;
  26.   }
  27. /*
  28.  * Given that one corner (DRB) is in the correct position,
  29.  * move the other corners into the correct position.  There
  30.  * are (according to Taylor), 4 possibilities, clockwise
  31.  * rotation of the three other corners, counterclockwise
  32.  * rotation, horizontal exchange (of two), or diagonal
  33.  * exchange of two.
  34.  */
  35.   if ( CornerCorrect(D,R,F) ) {            /* DRF correct */
  36.     if ( CornerCorrect(D,L,F) ) {          /* DLF correct */
  37.       ;
  38.     } else {
  39.       R1D3L3D1R3D3L1D2;           /* Exchange DLF and DLB */
  40.     }
  41.   } else {
  42.     if ( CornerCorrect(D,L,F) ) {
  43.       D1B1D1R1D3R3B3;             /* Exchange DLB and DRF */
  44.     } else {
  45.       if ( CornerCorrect(D,L,B) ) {
  46.         B1D3F3D1B3D3F1D2;         /* Exchange DLF and DRF */
  47.       } else { 
  48.         if ( CornerEquals(D,L,B,D,L,F) ) {
  49.           L3D1R1D3L1D1R3D3;              /* DLF<-DLB<-DRF */
  50.         } else {
  51.           D1R1D3L3D1R3D3L1;              /* DLF<-DRF<-DLB */
  52.         }
  53.       }
  54.     }
  55.   }
  56. /**********************************************************
  57.  * 
  58.  * STEP 5:  Twist corners in bottom layer.
  59.  *
  60.  *********************************************************/
  61.   loopCount=0;
  62.   do {
  63.     if (++loopCount > 16) return (false);
  64. /*  
  65.  *  At this point, all the corners in the bottom layer are
  66.  *  in the correct positions, but perhaps not in the 
  67.  *  correct orientation.  
  68.  */
  69.     if (DLF_F == F && DRF_R == R &&
  70.         DRB_B == B  && DLB_L == L)
  71.       break;
  72. /*  
  73.  *  Not all of the corners are in the correct orientation.
  74.  *  The cube has the property that the "twist" of corner
  75.  *  cubes sums to zero, meaning that we have one of the
  76.  *  following cases:
  77.  *  -- 3 cubes needing a clockwise twist
  78.  *  -- 2 cubes needing a clockwise twist and
  79.  *       2 needing a counterclockwise twist
  80.  *  -- 1 cube needing a counterclockwise twist and
  81.  *       1 needing a clockwise twist
  82.  *  -- 3 cubes needing a counterclockwise twist
  83.  *  The operators used in this cube solution twist one
  84.  *  corner clockwise and one counterclockwise.
  85.  */
  86.     if (DLF_F == D) {  /* DLF needs a clockwise twist */
  87.       L3U1L1F1U1F3;
  88.  /* 
  89.   * Find a cube that needing a counterclockwise turn. 
  90.   */
  91.       if (DLB_B == D) {
  92. LabD1F1U3F3L3U3L1D3:
  93.         D1F1U3F3L3U3L1D3;
  94.       } else if (DRB_R == D) {
  95.         D2F1U3F3L3U3L1D2;
  96.       } else if (DRF_F == D) {
  97. LabD3F1U3F3L3U3L1D1:
  98.         D3F1U3F3L3U3L1D1;
  99.       } else {  /* No counterclockwise turn is needed, 
  100.                    so we make one arbitrarily */
  101.         if (DLB_D != D) {
  102.             goto LabD1F1U3F3L3U3L1D3;
  103.         } else {
  104.             goto LabD3F1U3F3L3U3L1D1;
  105.         }
  106.       }
  107.     } else if (DRF_R == D) {/*DRF needs a clockwise twist*/
  108.       F3U1F1R1U1R3;
  109.       if (DLF_L == D) {
  110. LabD1R1U3R3F3U3F1D3:
  111.         D1R1U3R3F3U3F1D3;
  112.       } else if (DLB_B == D) {
  113.         D2R1U3R3F3U3F1D2;
  114.       } else if (DRB_R == D) {
  115. LabD3R1U3R3F3U3F1D1:
  116.         D3R1U3R3F3U3F1D1;
  117.       } else {  /* No counterclockwise turn is needed, 
  118.                    so we make one arbitrarily */
  119.         if (DLF_D != D) {
  120.             goto LabD1R1U3R3F3U3F1D3;
  121.         } else {
  122.             goto LabD3R1U3R3F3U3F1D1;
  123.         }
  124.       }
  125.     } else if (DRB_B == D) {/*DRB needs a clockwise twist*/
  126.       R3U1R1B1U1B3;
  127.       if (DRF_F == D) {
  128. LabD1B1U3B3R3U3R1D3:
  129.         D1B1U3B3R3U3R1D3;
  130.       } else if (DLF_L == D) {
  131.         D2B1U3B3R3U3R1D2;
  132.       } else if (DLB_B == D) {
  133. LabD3B1U3B3R3U3R1D1:
  134.         D3B1U3B3R3U3R1D1;
  135.       } else {  /* No counterclockwise turn is needed, 
  136.                    so we make one arbitrarily */
  137.         if (DRF_D != D) {
  138.             goto LabD1B1U3B3R3U3R1D3;
  139.         } else {
  140.             goto LabD3B1U3B3R3U3R1D1;
  141.         }
  142.       }
  143.     } else if (DLB_L == D) {/*DLB needs a clockwise twist*/
  144.       B3U1B1L1U1L3;
  145.       if (DRB_R == D) {
  146. LabD1L1U3L3B3U3B1D3:
  147.         D1L1U3L3B3U3B1D3;
  148.       } else if (DRF_F == D) {
  149.         D2L1U3L3B3U3B1D2;
  150.       } else if (DLF_L == D) {
  151. LabD3L1U3L3B3U3B1D1:
  152.         D3L1U3L3B3U3B1D1;
  153.       } else {  /* No counterclockwise turn is needed, 
  154.                    so we make one arbitrarily */
  155.         if (DRB_D != D) { 
  156.           goto LabD1L1U3L3B3U3B1D3;
  157.         } else {          
  158.           goto LabD3L1U3L3B3U3B1D1;
  159.         }
  160.       }
  161.     } else {
  162. /*
  163.  * There are no corner cubes that need a clockwise twist.  
  164.  * So there must be 3 needing a counterclockwise twist.  
  165.  * We twist one clockwise and one counterclockwise.
  166.  */
  167.       if (DLF_F != F) {
  168.         L3U1L1F1U1F3D1F1U3F3L3U3L1D3;
  169.       } else {
  170.         F3U1F1R1U1R3D1R1U3R3F3U3F1D3;
  171.       }
  172.     }
  173.   } while(true);
  174.   return (true);
  175. }
  176.